package org.hao.samples.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.hao.samples.model.OneSubjectVO;
import org.hao.samples.model.Student;
import org.hao.samples.model.Subject;
import org.hao.samples.model.SubjectVO;
import org.hao.samples.model.commonMessageResultVO;
import org.hao.samples.model.scoreVO;
import org.hao.samples.service.StudentService;
import org.hao.samples.service.subjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class subjectController {

	@Autowired
	private subjectService service;
	
	@Autowired
	private StudentService studentService;

	@RequestMapping("/subjects")
	public String subjects(Model model) {
		List<Subject> list = service.findAllSubject();
		model.addAttribute("subjectList", list);
		return "subject/subjectList";
	}
	
	@RequestMapping(value = "/deleteSubject/{id}", method = RequestMethod.GET)
	public String delete(RedirectAttributes redirectAttributes, @PathVariable Integer id) {
		try {
			service.delete(id);
			redirectAttributes.addFlashAttribute("message", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		return "redirect:/subjects";
	}
	
	@ResponseBody
	@RequestMapping(value = "/getSubjectById",method =  RequestMethod.POST)
	public SubjectVO getSubjectById(Integer sub_id){
		SubjectVO SubjectVO = service.getSubjectVOById(sub_id);
		return SubjectVO;
	}
	
	@ResponseBody
	@RequestMapping(value = "/saveSubject",method =  RequestMethod.POST)
	public commonMessageResultVO saveSubject(@Valid SubjectVO subjectVO,BindingResult bindingResult){
		commonMessageResultVO resultVO = new commonMessageResultVO();
		try {
			service.saveExamTest(subjectVO);
			resultVO.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultVO;
	}
	
	@ResponseBody
	@RequestMapping(value = "/editSubject",method =  RequestMethod.POST)
	public commonMessageResultVO editTest(@Valid SubjectVO subjectVO,BindingResult bindingResult){
		commonMessageResultVO resultVO = new commonMessageResultVO();
		try {
			service.editTest(subjectVO);
			resultVO.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultVO;
	}
	
	/**
	 * 获取所有当前科目关联的学生后台方法
	 * @param subjectVO
	 * @param bindingResult
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getAllStudent")
	public Map<String,Object> getAllStudent(Integer subjectId){
		HashMap<String, Object> resulMap = new HashMap<String,Object>();
		resulMap.put("allUser", studentService.findAllStudentVO());
		
		List<Student> list = service.getAllUserBySubject(subjectId);
		List<Integer> studentIdList = new ArrayList<Integer>();
		for (int i = 0; i < list.size(); i++) {
			studentIdList.add(list.get(i).getUser_id());
		}
		resulMap.put("selectedUser", studentIdList);
		return resulMap;
	}
	
	@ResponseBody
	@RequestMapping(value = "/saveSubjectStudents")
	public Map<String,Object> saveSubjectStudents(@RequestParam(name="subjectStudents[]")String[] subjectStudents,Integer subjectId){
		HashMap<String, Object> resultMap = new HashMap<String,Object>();
		resultMap.put("success", false);
		List<Integer> list = new ArrayList<Integer>();
		for(String student : subjectStudents){
			list.add(Integer.parseInt(student));		
		}
		
		service.saveSubjectStudent(list,subjectId);
		
		return resultMap;
	}
	
	/**
	 * 获取所有当前学生所有的科目后台方法
	 * 
	 * @param subjectVO
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(value = "/findSubjectByStudent")
	public String findAllSubjectByStudent(Model model,HttpServletRequest request) {
		HttpSession session = request.getSession();
		Student examUser = (Student) session.getAttribute("examUser");
		List<OneSubjectVO> subject = service.findAllSubjectByStudent(examUser.getUser_id());
		model.addAttribute("subjectList", subject);
		return "student/subjectList";
	}
	
	/**
	 * 获取当前学生科目成绩
	 */
	@ResponseBody
	@RequestMapping(value = "/showScore",method =  RequestMethod.POST)
	public scoreVO showScore(HttpServletRequest request,Integer scoreId) {
		HttpSession session = request.getSession();
		Student examUser = (Student) session.getAttribute("examUser");
		return service.findScoreById(scoreId,examUser.getUser_name());	
	}
}
